<?php

namespace admin\widgets;

use yii\base\Widget;
use Yii;
use yii\helpers\Html;
use yii\helpers\Url;

class AdminToolbar extends Widget {

    public $search = FALSE;
    public $saveButton = TRUE;
    public $dialog = TRUE;
    public $name = '';
    public $placeholder = '';

    public function run() {
        $url = Url::to(['save']);

        if (empty($this->name)) {
            $name = Yii::$app->controller->name;
            if (strpos($name, '管理员') === FALSE) {
                $name = str_replace('管理', '', $name);
            }
            $this->name = '添加' . $name;
        }

        $button = FALSE;
        if ($this->saveButton) {
            if ($this->dialog) {
                $button = Html::button($this->name, ['class' => 'btn dialog', 'data-href' => $url]);
            } else {
                $button = Html::a($this->name, $url, ['class' => 'btn']);
            }
        }

        return Yii::$app->controller->renderPartial('/widget/toolbar', [
            'search' => $this->search,
            'button' => $button,
            'placeholder' => $this->placeholder
        ]);
    }
}